<?php

namespace backend\controllers;

use common\models\CdnConfig;
use Yii;
use yii\data\ArrayDataProvider;
use yii\db\Exception;
use yii\helpers\FileHelper;
use yii\web\Response;

/**
 * Class SystemController
 * @package backend\controllers
 */
class SystemController extends BaseController
{
    /**
     * actionCache
     * @return string|Response
     * @throws Exception
     */
    public function actionCache()
    {
        $type = get('type',0);

        switch ($type){
            case 1:
                cache()->flush();
                $this->sessionSuccess('清空所有缓存成功!');
                return $this->redirect(['cache']);
            case 2:
                db()->schema->refresh();
                $this->sessionSuccess('清空全部表结构缓存成功!');
                return $this->redirect(['cache']);
            case 3:
                $tableName = get('tableName',null);
                db()->schema->refreshTableSchema($tableName);
                $this->sessionSuccess('清空 ' . $tableName . ' 表缓存成功!');
                return $this->redirect(['cache']);
            case 4:
                $assetsBasePath = Yii::getAlias(app()->assetManager->basePath);
                $assetsPaths = FileHelper::findDirectories($assetsBasePath,['recursive'=>false]);
                foreach ($assetsPaths as $assetsPath){
                    FileHelper::removeDirectory($assetsPath);
                }
                $this->sessionSuccess('清空 assets 缓存成功!');
                return $this->redirect(['cache']);
            default:
                break;
        }

        $dataProvider = new ArrayDataProvider([
            'allModels' => db()->createCommand('SHOW TABLE STATUS')->queryAll(),
            'pagination' => [
                'pageSize' => 0
            ],
        ]);

        return $this->render('cache',[
            'dataProvider' => $dataProvider
        ]);
    }

    /**
     * OpCache
     * actionOcp
     * @return string
     */
    public function actionOcp()
    {
        return $this->render('ocp');
    }

    /**
     * actionCdn
     * @return string|Response
     * @throws \yii\web\BadRequestHttpException
     */
    public function actionCdn()
    {
        $class = get('class');
        if(empty($class)){
            $model = CdnConfig::find()->one();
        }else{
            if(empty($model = CdnConfig::findOne(['class'=>$class]))){
                $model = new CdnConfig();
                if(!in_array($class,$model->classArray,1)){
                    throw new \yii\web\BadRequestHttpException('请求参数错误.');
                }
                $model->class = $class;
                $model->save(false);
                $model->loadDefaultValues();
            }
        }

        if(request()->isPost && $model->load(post())){
            if($model->status == 10){
                CdnConfig::updateAll(['status'=>0]);
            }
            if($model->save()){
                $this->sessionSuccess();
                return  $this->refresh();
            }else{
                $this->sessionError($model->getFirstErrorMsg('配置云存储错误.'));
            }
        }

        return $this->render('cdn',[
            'model' => $model
        ]);
    }

}
